package com.github.zzay.admin.controller;

import com.github.zzay.admin.dto.AdminDto;
import com.github.zzay.admin.service.AdminService;
import com.github.zzay.common.core.model.ResponseBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zzay
 * @className AdminController
 * @description Controller class of Admin user.
 * @create 2022/07/05 16:16
 */
@Slf4j
@RestController
@RequestMapping("/admin")
@CrossOrigin
@Tag(name = "AdminController", description = "管理员信息操作相关接口")
public class AdminController {

    /**
     * Admin Service.
     */
    @Autowired
    private AdminService adminService;

    /**
     * Register an admin user.
     *
     * @param adminDto Admin DTO
     * @return Operation Result
     */
    @Operation(summary = "用户注册", description = "用户注册：用户通过注册表单输入用户名和密码，执行注册操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "账号注册成功，直接登录"),
            @ApiResponse(responseCode = "400", description = "用户名已被注册，需要重新注册")
    })
    @PostMapping("/register")
    public ResponseBean<Map<String, Object>> register(
            @Parameter(name = "adminDto") @RequestBody AdminDto adminDto) {
        log.info("REGISTERING...");
        Map<String, Object> data = new HashMap<>();
        Boolean success = adminService.register(adminDto);
        data.put("success", success);
        // check whether the username has been used or not
        if (success) {
            return new ResponseBean<>(HttpStatus.OK.value(), "Register successfully and log in", data);
        }
        // if the username has been used, ask the user to re-register
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "The username has been used, please try another one", data);
    }

    /**
     * Log-in of an admin user.
     *
     * @param adminDto Admin DTO
     * @return Operation Result
     */
    @Operation(summary = "用户登录", description = "用户登录：用户通过登录表单输入用户名和密码，执行登录操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "用户登录成功"),
            @ApiResponse(responseCode = "400", description = "用户名或密码错误，需要重新登录")
    })
    @PostMapping("/login")
    public ResponseBean<Map<String, Object>> login(
            @Parameter(name = "adminDto") @RequestBody AdminDto adminDto) {
        Map<String, Object> data = new HashMap<>();
        Boolean success = adminService.login(adminDto);
        data.put("success", success);
        if (success) {
            return new ResponseBean<>(HttpStatus.OK.value(), "Log in successfully", data);
        }
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Username or password is incorrect, please retry", data);
    }

    /**
     * Log-out of an admin user.
     *
     * @param adminDto Admin DTO
     * @return Operation Result
     */
    @Operation(summary = "用户登出", description = "用户登出：用户通过点击登出按钮，执行登出操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "用户登出成功"),
            @ApiResponse(responseCode = "400", description = "用户登出失败")
    })
    @PostMapping("/logout")
    public ResponseBean<Map<String, Object>> logout(
            @Parameter(name = "adminDto") @RequestBody AdminDto adminDto) {
        Map<String, Object> data = new HashMap<>();
        Boolean success = adminService.logout(adminDto);
        data.put("success", success);
        if (success) {
            return new ResponseBean<>(HttpStatus.OK.value(), "Log out successfully", data);
        }
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to log out", data);
    }

}
